﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using System.Reflection;
using System.IO;

namespace Plugin
{
    public class Config
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        private string _ServiceName = "33所装箱运输单打印";

        public string ServiceName
        {
            get
            {
                return _ServiceName;
            }
        }

        /// <summary>
        /// 插件版本
        /// </summary>
        private string _DLLVersion = "1.1";

        public string DLLVersion
        {
            get
            {
                return _DLLVersion;
            }
        }

        /// <summary>
        /// 运行间隔，默认60秒执行一次
        /// </summary>
        private int _RunInterval = 60;

        public int RunInterval
        {
            get
            {
                return _RunInterval;
            }
        }

        /// <summary>
        /// 显示设置窗体
        /// </summary>
        public void ShowConfig()
        {
            frmConfig newFrmConfig = new frmConfig();
            newFrmConfig.Show();
        }
    }

    public class Service
    {
        private SqlTools sqt;

        public Service ()
        {
            //初始化SQL查询对象
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
        }

        public void Start()
        {
            //检查当前是否有33所、飞航物资销货单
            string strTmpSelectString = "select 销货单号,序号,采购文件编号,规格型号,批次,生产日期,数量,备注 from (select xhb001 销货单号,xhb002 销货项次,'' 序号,htb027 采购文件编号,xhb003 规格型号,gxck017 批次,gxck021 生产日期,xhb008 数量,'' 备注 from orxha_table,orxhb_table,gxck_table,htb_table where xha001=xhb001 and left(xhb014,12)=gxck001 and gxck010=htb001 and gxck011=htb002 and xha008 like '中国航天科工飞航技术研究院%' and xha013='Y' and xha001 not in (select 销货单号 from zxysd_33) and xhb032='603' union all select xhb001 销货单号,xhb002 销货项次,'' 序号,htb027 采购文件编号,xhb003 规格型号,gxck017 批次,gxck021 生产日期,xhb008 数量,'' 备注 from orxha_table,orxhb_table,TPAMainTZ.dbo.gxck_table,htb_table where xha001=xhb001 and left(xhb014,12)=gxck001 and gxck010=htb001 and gxck011=htb002 and xha008 like '中国航天科工飞航技术研究院%' and xha013='Y' and xha001 not in (select 销货单号 from zxysd_33) and xhb032='601') a order by 销货单号,销货项次";
            DataTable tmpdt = sqt.GetData(strTmpSelectString);
            if (tmpdt!=null && tmpdt.Rows.Count >0)
            {
                try
                {
                    List<DataTable> lstDt = new List<DataTable>();
                    //生成报表数据
                    DataTable dt = new DataTable();
                    int iCount = 1;
                    string strTmpXhNo = string.Empty;
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        if (!tmpdt.Rows[i]["销货单号"].Equals(strTmpXhNo))
                        {
                            //当前行销货单号与缓存销货单号不一致时，保存当前缓存数据，更新销货单号
                            if (dt.Rows.Count > 0)
                            {
                                lstDt.Add(dt);
                            }
                            //声明新数据表
                            dt = new DataTable();
                            dt.Columns.Add("销货单号");
                            dt.Columns.Add("序号");
                            dt.Columns.Add("采购文件编号");
                            dt.Columns.Add("规格型号");
                            dt.Columns.Add("批次");
                            dt.Columns.Add("生产日期");
                            dt.Columns.Add("数量");
                            dt.Columns.Add("备注");
                            //缓存销货单号
                            strTmpXhNo = GeneralTools.GetString(tmpdt.Rows[i]["销货单号"], string.Empty);
                            //初始化行标
                            iCount = 1;
                        }
                        //将当前行添加到数据表
                        DataRow tmpdr = dt.NewRow();
                        tmpdr["销货单号"] = tmpdt.Rows[i]["销货单号"];
                        tmpdr["序号"] = iCount;
                        tmpdr["采购文件编号"] = tmpdt.Rows[i]["采购文件编号"];
                        tmpdr["规格型号"] = tmpdt.Rows[i]["规格型号"];
                        tmpdr["批次"] = tmpdt.Rows[i]["批次"];
                        tmpdr["生产日期"] = tmpdt.Rows[i]["生产日期"];
                        tmpdr["数量"] = tmpdt.Rows[i]["数量"];
                        tmpdr["备注"] = tmpdt.Rows[i]["备注"];
                        dt.Rows.Add(tmpdr);
                        iCount++;
                        if (i == tmpdt.Rows.Count - 1)
                        {
                            //如果已读取到最后一行，则保存数据表
                            lstDt.Add(dt);
                        }
                    }
                    ReportDocument tmprd = new ReportDocument();
                    StringBuilder tmpsb = new StringBuilder();
                    GeneralTools.GetPrivateProfileString("Config", "Printer", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_33_Config.ini");
                    string strTmpPrinterName = tmpsb.ToString();
                    GeneralTools.GetPrivateProfileString("Config", "CopyCount", "1", tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_33_Config.ini");
                    int iCopyCount = GeneralTools.GetInt(tmpsb.ToString(), 1);
                    tmprd.Load(System.Windows.Forms.Application.StartupPath + "\\Plugin\\Report.rpt");
                    for (int i = 0; i < lstDt.Count; i++)
                    {
                        tmprd.SetDataSource(lstDt[i]);
                        tmprd.PrintOptions.PrinterName = strTmpPrinterName;
                        tmprd.PrintToPrinter(iCopyCount, false, 0, 0);
                        sqt.DoCmdExecuteNonQuery("insert into zxysd_33 (销货单号) values ('" + GeneralTools.GetString(lstDt[i].Rows[0]["销货单号"], string.Empty) + "')");
                    }
                }
                catch (Exception ex)
                {
                    GeneralTools.ShowErrorMsg("出现错误：" + ex.Message);
                }
            }
        }
    }
}
